Track = function()
{
  this.info = [];
  this.index;
  this.target;
}

Track.prototype.setTarget = function(dom_elem)
{
    this.target = dom_elem;
}

Track.prototype.checkLink = function(url)
{
  var track_info = this.info[this.index];
  var self = this;
  var oncompl = function(o){ 

    var r = o.responseXML.getElementsByTagName('proxy')[0].firstChild.nodeValue;

    try{
      if(r != 'OK')
      {
        mt.track.info[mt.track.index]['url'] = false;
        self.callback(url, {msg:'BAD_LINK',value:r});//
       }
       else
       {
         mt.track.info[mt.track.index]['url'] = url;
         self.callback(url);
        }
      }catch(e){ alert(e)  }
  }
  var options = {target: this.target, timeout: 0,
          onLoading:function(o, target){ mt.onLoading(target,'Verifying the link provided...'); },
          onError: function(o,target,str){ throw str; },
          onComplete: function(o,targ){oncompl(o);},        
          data: 'url='+url+'&op=link_check'};

   new Ajax().makeRequest('proxy.php', options);
}

Track.prototype.select = function(index)
{
  this.index = index;
}
Track.prototype.finished = function(track_id)
{
  this.info[this.index]['id'] = track_id;	
}

Track.prototype.add = function()
{
  var postdata = mt.serialize(this.info[this.index]);
  if(mt.article.body)
      postdata += '&article='+mt.article.body;
  
  var self = this;
  var oncomplete = function(o){
          try{ 
            var id = o.responseXML.getElementsByTagName('result')[0];

            if(id){
              self.finished(id.firstChild.nodeValue);
              self.callback(id.firstChild.nodeValue);
            }
            else{
              var errorFields =  o.responseXML.getElementsByTagName('errorFields')[0];
              if(errorFields){
               self.callback(null,{msg:"ERROR_FIELDS",value:errorFields.firstChild.nodeValue});
              }
              else throw "PROXY_ERROR";
            }
          }catch(e){
              self.callback(null,{msg:"PROXY_ERROR", value:null});
          }
        }
  var options = {target: this.target, timeout: 0, 
        onLoading:function(o,target){ mt.onLoading(target,'Adding song to our Data Base...'); },
        onError: function(o,target,str){ throw str; },
        onComplete: function(o,target){ oncomplete(o); },        
        data: postdata+'&op=add'};

 new Ajax().makeRequest('index.php?view=track&action=add', options); 
}

Track.prototype.force = function()
{
  this.info[0] = { title: $('title').getValue().trim().uword(), artist: $('artist').getValue().trim().uword(), release: null}
}
